<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>修改角色</legend>
</fieldset>
<div class="layui-form-item">
    <div class="layui-inline" >
        <form class="layui-form"  lay-filter="example"  >
            <input  id="id" name="id" style="display: none">
            <input  id="rid" name="rid" style="display: none">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="username" name="username" lay-verify="username"  readonly   class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户角色</label>
                    <div class="layui-input-inline">
                        <select name="rid" id="role" lay-filter="role">
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit lay-filter="userForm">保存修改</button>
                </div>
            </div>
      </form>
    </div>
</div>
<script  src="../../../layui/layui.js" ></script>
<script src="../../../layui/layui.all.js"  ></script>
<script src="../../../js/jquery.min.js"></script>
<script src="../../../js/common.js"></script>
<script>
    layui.use('table', function() {
                var table = layui.table;
                var form = layui.form;
                var layer = layui.layer;
                var userId =getUrlParam("userId");
                var username =getUrlParam("username");

                 //先执行加载函数，再执行回显函数formRender();
                 $(function(){
                     $.get("/sysRole/findAll",{},function (data) {
                         console.log("22222"+data);
                         var list = data.data;
                         var select = document.getElementById('role');
                         var option1 = document.createElement("option");
                         option1.innerText = "请选择"
                         option1.setAttribute("value", "");
                         select.appendChild(option1)
                         console.log(list);
                         if (list != null || list.size() > 0) {
                             for (var c in list) {
                                 var option = document.createElement("option");
                                 option.setAttribute("value", list[c].id);
                                 option.innerText = list[c].name;
                                 select.appendChild(option)
                             }
                             form.render('select');
                             formRender();
                         };
                     },"json");
                 })
                form.val('example', {
                    "id": userId// "name": "value"
                    ,"username": username // "name": "value"
                });

                 function formRender(){
                     $.ajax({
                         url:"/sysUser/findRoleIdByUId?uid="+userId,
                         method:"post",
                         success:function(data){
                             console.log("11111"+data);
                             if(data.code ==1){
                                 $("#role option[value="+data.data+"]").attr('selected','selected');
                                 form.render('select');
                             }
                         }
                     });
                 }


                    //监听selcect
                form.on('select(role)', function(data) {
                    $("#rid").attr("value",data.value)
                    console.log(data.elem); //得到select原始DOM对象
                    console.log(data.value); //得到被选中的值]
                    console.log(data.elem[data.elem.selectedIndex].text); //得到select选中值的文本内容
                });

                 //监听提交
                 form.on('submit(userForm)', function(data){
                     if(data.field.username == "admin"){

                         layer.msg("该用户不允许操作！")
                     }else{

                         console.log(data.field);
                      $.ajax({
                             url:"/sysUser/updateRole",
                             method:"post",
                             data:{"uid":data.field.id,"rid":data.field.rid},
                             success:function(data){
                                 if(data.code== 404 ){
                                     layer.msg(data.msg);
                                 }else {
                                     layer.msg(data.msg);
                                     setTimeout(function(){
                                         layer.close(layer.index);
                                         window.parent.location.reload();
                                     },1800 );

                                 }
                             }
                         });
                     }
                 });
             })

</script>
</body>
</html>